<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\order\Day;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $admin = Db::name("admin")->where('id',$this->auth->id)->find();
        $mechId = $admin['mechId'];
        $appId = $admin['appId'];

        $assign = [
            "today_rujin_num" =>  0,
            "today_chujin_num" =>  0,
            "today_rujin_money" =>  0,
            "today_chujin_money" =>  0,
            "today_shouyi" =>  0,
            "yes_rujin_num" =>  0,
            "yes_chujin_num" =>  0,
            "yes_rujin_money" =>  0,
            "yes_chujin_money" =>  0,
            "yes_shouyi" =>  0,
        ];
        $map = [
            'txnStatus' => 'S',
            'status' => '1',
        ];

        if ($this->auth->id != 1){
            $map['mechId'] = $mechId;
            return '';
        }
        $map['store_id'] = ['in',[3,5,7]];
        $assign = $this->get_assign($map);
        foreach ($assign as $k=> $v){
            $this->assign($k,$v);
        }

        $map['store_id'] = ['in',[91,92]];
        $assign2 = $this->get_assign($map);
        $this->assign("assign2",$assign2);

        return $this->view->fetch();
    }

    private function get_assign($map){
        $assign = [
            "today_rujin_num" =>  0,
            "today_chujin_num" =>  0,
            "today_rujin_money" =>  0,
            "today_chujin_money" =>  0,
            "today_shouyi" =>  0,
            "yes_rujin_num" =>  0,
            "yes_chujin_num" =>  0,
            "yes_rujin_money" =>  0,
            "yes_chujin_money" =>  0,
            "yes_shouyi" =>  0,
        ];

        $assign['today_rujin_num'] = Db::name("order")->where($map)->where("tradeType",1)->whereTime('createtime',"today")->count();
        $assign['today_chujin_num'] = Db::name("order")->where($map)->where("tradeType",2)->whereTime('createtime',"today")->count();
        $assign['today_rujin_money'] = Db::name("order")->where($map)->where("tradeType",1)->whereTime('createtime',"today")->sum("txnAmount");
        $assign['today_chujin_money'] = Db::name("order")->where($map)->where("tradeType",2)->whereTime('createtime',"today")->sum("txnAmount");

        $assign['today_shouyi'] = $assign['today_rujin_money'] * 1 / 10000;

        $assign['yes_rujin_num'] = Db::name("order")->where($map)->where("tradeType",1)->whereTime('createtime',"yesterday")->count();
        $assign['yes_chujin_num'] = Db::name("order")->where($map)->where("tradeType",2)->whereTime('createtime',"yesterday")->count();
        $assign['yes_rujin_money'] = Db::name("order")->where($map)->where("tradeType",1)->whereTime('createtime',"yesterday")->sum("txnAmount");
        $assign['yes_chujin_money'] = Db::name("order")->where($map)->where("tradeType",2)->whereTime('createtime',"yesterday")->sum("txnAmount");
        $assign['yes_shouyi'] = $assign['yes_rujin_money'] * 1 / 10000;
        return $assign;
    }

    public function trade(){
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        $model = new Day;
        $where = [];
        $store_id = input('store_id','');
        $day = 7;
        if(!$this->auth->isSuperAdmin()){
            $store_id = $this->auth->id;
        }else{
            $day = 90;
        }
        if($store_id > 0){
           $where['store_id'] = $store_id;
        }
        $return = [];
        for($i=$day;$i>=1;$i--){
            $temp = ['订单数'=> 0, '收益'=> 0, '交易量'=>0];
            $data = $model->where($where)->whereTime('createtime','between',[date('Y-m-d 00:00:00', strtotime('-'.$i.' days')),date('Y-m-d 23:59:59', strtotime('-'.$i.' days'))])->select();
            if($data){
                foreach($data as $v){
                    $temp['收益'] += $v->shouyi;
                    $temp['订单数']  += $v->runum;
                    $temp['交易量']  += $v->ru;
                }
            }
            $return[date('m-d', strtotime('-'.$i.' days'))] = $temp;
        }
        return $this->success('', null, $return);

    }

}
